/*
 * z80_instructions.hpp
 *
 *  Created on: Jan 4, 2012
 *      Author: douglas
 */

//Bibliography
//[1] - Programming the Z80 - 3th ed. - Rodnay Zaks



/*
 - Accessing memory takes 3 cycles.
 - Four clock cycles for the Fetch, for all instructions. So each instruction has at least 4 clock cycles.


 */


union z80_instruction
{
	u16 hex;

	z80_instruction(u16 _hex)	{ hex = _hex;}
	z80_instruction()			{ hex = 0;}

	//instructions using this fields:
	//ADC_n, ADC_HL, ADC_IX_d, ADC_IY_d, ADC_HL_SS
	struct
	{
		u8 opcode8       : 8;
		u8               : 8;
	};


	//instructions using this fields:
	//ADC_r
	struct
	{
		u8 sourceReg     : 3;
		u8 bitNumber     : 3;
		u8               : 2;
		u8               : 8;
	};

	//instructions using this fields:
	//ADC_HL_SS
	struct
	{
		u8 opcode2_1     : 4;
		u8 sourceRegPair : 2;
		u8 opcode4_2     : 2;
		u8               : 8;
	};

	struct
	{
		u8               : 4;
		u8 destRegPair   : 2;
		u8               : 2;
		u8               : 8;
	};

	struct
	{
		u8               : 3;
		u8 condition     : 3; //Condition used in jumps, etc
		u8               : 2;
		u8               : 8;
	};

	struct
	{
		u8               : 3;
		u8 condition_rel : 2; //Condition used in relative jumps, etc. Only 2 bits, but compatible with 3 bits condition
		u8               : 3;
		u8               : 8;
	};

	struct
	{
		u8               : 3;
		u8 regToDec      : 3;
		u8               : 2;
		u8               : 8;
	};

	struct
	{
		u8               : 3;
		u8 destReg       : 3;
		u8               : 2;
		u8               : 8;
	};



	inline operator unsigned() const { return hex; }
	inline unsigned operator  == (unsigned i) { return hex  == i; }
	inline unsigned operator  == (signed i)   { return hex  == i; }
	inline unsigned operator   = (unsigned i) { return hex   = i; }
	inline unsigned operator  |= (unsigned i) { return hex  |= i; }
	inline unsigned operator  ^= (unsigned i) { return hex  ^= i; }
	inline unsigned operator  &= (unsigned i) { return hex  &= i; }
	inline unsigned operator <<= (unsigned i) { return hex <<= i; }
	inline unsigned operator >>= (unsigned i) { return hex >>= i; }
	inline unsigned operator  += (unsigned i) { return hex  += i; }
	inline unsigned operator  -= (unsigned i) { return hex  -= i; }
	inline unsigned operator  *= (unsigned i) { return hex  *= i; }
	inline unsigned operator  /= (unsigned i) { return hex  /= i; }
	inline unsigned operator  %= (unsigned i) { return hex  %= i; }
};
















